package com.ziyun.erp.web.modules.purchase.controller;


import com.ziyun.erp.common.e.purchase.PurchaseApplyFinanceStatusEnum;
import com.ziyun.erp.common.e.purchase.PurchaseApplyTaxEnum;
import com.ziyun.erp.common.utils.JsonUtils;
import com.ziyun.erp.modules.purchase.entity.dto.StorePurchaseApplyDto;
import com.ziyun.erp.web.modules.capital.controller.QueryPage;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.UserUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ziyun.erp.modules.purchase.entity.StorePurchaseApplyEntity;
import com.ziyun.erp.modules.purchase.service.StorePurchaseApplyService;
import io.renren.common.utils.R;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;


/**
 * 门店采购申请表
 *
 * @author guizhao
 * @email 
 * @date 2018-09-26 13:28:47
 */

@RestController
@RequestMapping("purchase/storepurchaseapply")
public class StorePurchaseApplyController {
    @Autowired
    private StorePurchaseApplyService storePurchaseApplyService;

    /**
     * 列表
     */
    @RequestMapping("/querylistPage")
    @RequiresPermissions("purchase:storepurchaseapply:list")
    public R querylistPage(@RequestBody QueryPage<StorePurchaseApplyEntity> query){
        if(query.getParams().get("applyNo") != null){
            query.getPage().setCurrent(1);
        }
        storePurchaseApplyService.querylistPage(query.getPage(),query.getParams());
        return R.ok().put("page", query.getPage());
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{applyId}")
    @RequiresPermissions("purchase:storepurchaseapply:info")
    public R info(@PathVariable("applyId") Integer applyId){
        StorePurchaseApplyEntity storePurchaseApply = storePurchaseApplyService.selectById(applyId);

        return R.ok().put("storePurchaseApply", storePurchaseApply);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("purchase:storepurchaseapply:save")
    public R save(@RequestBody StorePurchaseApplyDto storePurchaseApplyDto){
        int sellerId = storePurchaseApplyDto.getStorePurchaseApplyEntity().getSellerId();
        storePurchaseApplyDto.setSellerId(sellerId);
        StorePurchaseApplyEntity storePurchaseApplyEntity = storePurchaseApplyDto.getStorePurchaseApplyEntity();
        storePurchaseApplyEntity.setCreateName(UserUtil.getCurrentUser().getUsername());
        return storePurchaseApplyService.insertApply(storePurchaseApplyDto);
    }

    /**
     * 审核
     */
    @RequestMapping("/audit")
    public R audit(@RequestBody StorePurchaseApplyDto storePurchaseApplyDto){
        return storePurchaseApplyService.audit(storePurchaseApplyDto);
    }


    /**
     * @author   Alix_zw
     * @Date
     * @Description   驳回
    **/
    @RequestMapping("/reject")
    public R reject(@RequestBody StorePurchaseApplyDto storePurchaseApplyDto){
        return storePurchaseApplyService.reject(storePurchaseApplyDto);
    }



    // 获取枚举
    @RequestMapping("enumlist")
    public R getEnum (){
        List<Map<String, Object>> purchaseApplyTaxEnum = JsonUtils.enumToList(PurchaseApplyTaxEnum.class);
        List<Map<String, Object>> purchaseApplyFinanceStatusEnum = JsonUtils.enumToList(PurchaseApplyFinanceStatusEnum.class);
        return R.ok().put("purchaseApplyTaxEnum",purchaseApplyTaxEnum).put("purchaseApplyFinanceStatusEnum",purchaseApplyFinanceStatusEnum);

    }

    @RequestMapping("/selectAllList")
    public R listGetPage(@RequestBody Map<String,Object> params){
        PageUtils page = storePurchaseApplyService.selectAll(params);
        R rs = R.ok();
        rs.put("page",page);
        return rs;
    }

    /**
     * @Description   采购报销审核
     * @Return    
    **/

    @RequestMapping("/Approval/{applyId}")
    public R Approval(@PathVariable("applyId") Integer applyId){
        Integer integer = storePurchaseApplyService.updateFinase(applyId);
        if(integer!=1){
            return R.error("审核失败");
        }
        return R.ok("审核成功");
    }

	/**
	 * @Description   采购报销复审
	 * @Return
	 **/
	@RequestMapping("/auditAgainCheck/{applyId}")
	public R auditAgainCheck(@PathVariable("applyId") Integer applyId){
		Integer integer = storePurchaseApplyService.updateFinaseCheck(applyId);
		if(integer!=1){
			return R.error("复审失败");
		}
		return R.ok("复审成功");
	}

    /**
     * @Description   采购报销单条付款
     * @Return
     **/
    @RequestMapping("/OnePay/{applyId}")
    public R OnePay(@PathVariable("applyId") Integer applyId){
        Integer integer = storePurchaseApplyService.updateFinaseCheckOnePay(applyId);
        if(integer!=1){
            return R.error("复审失败");
        }
        return R.ok("复审成功");
    }




    /**
     * @Description   采购报销批量审核
     * @Return
     **/
    @RequestMapping("/BatchAudit")
    public R BatchAudit(@RequestBody Object obj []){
        R r = storePurchaseApplyService.BatchAuditAll(obj);
        return r;
    }

    /**
     * @Description   采购报销批量付款
     * @Return
     **/
    @RequestMapping("/BulkPayment")
    public R BulkPayment(@RequestBody Object obj []){
        R r = storePurchaseApplyService.BulkPaymentAll(obj);
        return r;
    }


    /**
     * @Description   采购报销弃审
     * @Return
     **/
    @RequestMapping("/AbandonmentOfTrial/{applyId}")
    public R AbandonmentOfTrial(@PathVariable("applyId") Integer applyId){
        Integer integer = storePurchaseApplyService.updateFinaseN(applyId);
        if(integer!=1){
            return R.error("弃审失败");
        }
        return R.ok("弃审成功");
    }
}

